/*
 * spi_peripherals.c
 *
 *  Created on: 25.05.2012
 *      Author: Roman Leitner
 */

#include "../config/type.h"
#include "misc.h"
#include "lpc13xx.h"
#include "../driver/gpio.h"
#include "spi_peripherals.h"

int SSP_BUFSIZE = 8;

uint8_t src_addr[];
uint8_t dest_addr[];

void SpiInitCSN()
{

//	LPC_IOCON->PIO0_2 &= ~0x07;	/* SSP SSEL is a GPIO pin */
//	GPIOSetValue( PORT0, 2, 1 );
//	/* port0, bit 2 is set to GPIO output and high */
//	GPIOSetDir( PORT0, 2, 1 );

        //IOCON_PIO1_10 &= ~0x01;
        //IOCON_PIO1_10 &= ~0x01;	/* SSP SSEL is a GPIO pin */
	GPIOSetValue( PORT1, 10, 1 );
	/* port0, bit 2 is set to GPIO output and high */
	GPIOSetDir( PORT1, 10, 1 );
}

void SpiEnableDisplay()
{
	// Enable Chipselect/Load, low active
	GPIOSetValue( PORT1, 10, 0 );
}

spi_Error_t SpiDisableDisplay()
{
	// Disable Chipselect/Load, low active
	GPIOSetValue( PORT1, 10, 1 );
	return SPI_ERROR_OK;
}

spi_Error_t DisplayTest()
{
	src_addr[0] = AS1107_REG_DisplayTest;
	src_addr[1] = AS1107_DisplayTestMode;

	SpiEnableDisplay();
	SSPSend( (uint8_t *)src_addr, 2 );
	SpiDisableDisplay();

	return SPI_ERROR_OK;
}
spi_Error_t DisplayInit()
{

	uint8_t i;

	for ( i = 0; i < SSP_BUFSIZE; i++ )
	{
		src_addr[i] = 0;
		dest_addr[i] = 0;
	}

	// INit SPI, setup pins clk,
	SSPInit();
	// Init CSN pin
	SpiInitCSN();

	DisplayTest();
	return SPI_ERROR_OK;

}
